<?php
namespace src;
use src\sview;
use classlib\core as C;
class Asys {
	public static function main($funcode) {
		switch ($funcode) {
			case 1:
				self::__initialize();
				break;
		 	case 2:
				sview::verify();
				self::__vercheck();
				break;
			case 3:
				sview::verify(true, true);
				self::__imgupfile();
				break;
					
		/*	
			case 4:
				sview::verify();
				self::__dofeedback();
				break;
			case 5:
				sview::verify(true, true);
				self::__doclean();
				break;
			case 6:
				sview::verify();
				self::__doframe();
				break;
			case 7:
				sview::verify();
				self::__bootstrap();
				break;
			case 8:
				self::__olupdate();
				break;
			case 9:
				sview::verify();
				self::__docrashlog();
				break;
			case 10:
				sview::verify();
				self::__docrashfile();
				break;
			case 11:
				sview::verify();
				self::__doerrorlog();
				break; */
			default:
				sview::error(510, 'core', $_SERVER['HTTP_ACTION']);
		}
	}
	
	/**
	 * @desc 获取请求匿名签名0-1-1
	 * api?debug=1&___userid=1&action=0-1-1
	 */
	private static function __initialize() {
		$inc    = C\config::load('psocial', false);
		$static = \src\logic\lconfig::ddarr('system_staticuri');
		$hash   = '*'.sview::hash();
		$dataset= array('signature'=>$hash, 'currency'=>$inc['currency'], 'baseuri'=>$static[0][1]);
		sview::$view->assign_by_ref('datasets', $dataset);
		sview::$view->display();
	}
	
	/**
	 * @desc 版本升级验证0-1-2
	 * api?debug=1&action=0-1-2&type=1&appcode=live&version=0
	 */
	private static function __vercheck() {
		$where   = array();
		$otype   = isset(sview::$ctrl['type'])? intval(sview::$ctrl['type']):0;
		$appcode = isset(sview::$ctrl['appcode'])? strval(sview::$ctrl['appcode']):'';
		$version = isset(sview::$ctrl['version'])? intval(sview::$ctrl['version']):0;
		$where   = array('appcode'=>$appcode, 'ostype'=>$otype);
		$where[] = array('version', $version, \classlib\orm::OP_GT);
		$field   = 'memo,url as download,isupdate,vercode,filter';
		$list    = C\model::load('pro.inc.version')->get_list(0, -1, $where, $field, 'version', \classlib\orm::DESC);
		$dataset = array();
		if (!empty($list)) {
			foreach ($list as $item) {
				$filter = explode(',', $item['filter']);
				unset($item['filter']);
				//属于指定的版本，由端自动提示看看是否升级
				if (empty($dataset) && $filter && in_array($version, $filter)) {
					$dataset = $item;
					continue;
				}
				if ($dataset && $item['isupdate'] == 1) {
					$dataset['isupdate']= 1;
					continue;
				}
				if (empty($dataset)) {
					$dataset = $item;
				}
			}
		}
		if (empty($dataset)) {
			sview::$view->assign_error(80076, sview::error(80076, 'action'));
		}
		sview::$view->assign_by_ref('datasets', $dataset);
		sview::$view->display();
	}
	
	//图片资料上传0-1-3
	private static function __imgupfile() {
		$ctype  = isset($_SERVER['CONTENT_TYPE'])? $_SERVER['CONTENT_TYPE']:'';
		if (!$ctype || strcasecmp(substr($ctype, 0, 6), 'image/') != 0) {
			sview::$view->assign_error(80053, sview::error(80053, 'action'));
		}
		//参数 1、封面 2、头像 3、背景 4、相册图片 5、银行卡
		$otype  = (int)input::get('otype');
		$stype  = 'default';
		switch ($otype) {
			case 1:
				$stype = 'thumb';
				break;
			case 2:
				$stype = 'avtar';
				break;
			case 3:
				$stype = 'webbg';
				break;
			case 4:
				$stype = 'album';
				break;
			case 5:
				$stype = 'bank';
				break;
		}
		$userid = acl::getinst('vedio')->user['id'];
		$subfix = strtolower(substr($ctype, 6));
		$url    = upfile::dostream(sview::$view, sview::$origin, $subfix, $userid, $stype);
		if (($otype == 1 || $otype == 4) && $url) {//封面或者相册需要缩略图
			upfile::thumbnail($url, $userid, $stype);
		}
		sview::$view->assign('datasets', array('url'=>$url));
		sview::$view->display();
	}
	
	/* //在线更新配置0-1-8
	private static function __olupdate() {
		$conf   = config::load('psocial', false);
		$data = array('currency'=>$conf['currency'], 'baseuri'=>static_uri());
		sview::$view->assign_by_ref('datasets', $data);
		sview::$view->display();
	}
	
	//应用启动跟踪日志0-1-7
	private static function __bootstrap() {
		//设备码不存在 则直接退出不做绑定处理
		if (!isset($_SERVER['HTTP_DEVTOKEN'])) {
			sview::$view->display();
			exit;
		}
		$acl    = acl::getinst('vedio');
		$userid = ($acl->user)? $acl->user['id']:0;
		$device = $_SERVER['HTTP_DEVTOKEN'];
		$type   = (isset(sview::$ctrl['optype']) && sview::$ctrl['optype'] == 1)? 1:2;
		($userid < 1) && $type = 1;
		$adigg  = null;
		//app端的广告对接
		$adigg  = api_igguin::get($device, $userid, 1);
		$dataset = array('iggid'=>0);
		if ($adigg) {
			$ip     = get_ip();
			$stime  = config::$nowtime;
			$type   = (isset(sview::$ctrl['app']) && sview::$ctrl['app'] == 1)? 'a':'i';
			api_analysis_log::add_login_log($adigg['id'], $ip, $stime, $type);
			$dataset['iggid'] = intval($adigg['id']);
		}
		sview::$view->assign_by_ref('datasets', $dataset);
		sview::$view->display();
	}
	
	
	
	
	
	//意见反馈0-1-4
	private static function __dofeedback() {
		sview::$ctrl['clientid']  = intval(sview::$ctrl['clientid']);
		sview::$ctrl['os']  	  = trim(sview::$ctrl['os']);
		sview::$ctrl['version']   = trim(sview::$ctrl['version']);
		sview::$ctrl['msg']       = isset(sview::$ctrl['msg'])? sview::$ctrl['msg']:'';
		sview::$ctrl['msg']       = htmlspecialchars(sview::$ctrl['msg']);
		sview::$ctrl['attachs']   =  isset(sview::$ctrl['attachs'])? sview::$ctrl['attachs']:'';
		$userid                   = 0;
		$acl                      = acl::getinst('vedio');
		if ($acl->user) {
			$userid = $acl->user['id'];
		}
		//新增一条记录信息
		$sdata = array('clientid'=>sview::$ctrl['clientid'], 'userid'=>$userid, 'os'=>sview::$ctrl['os'],
				'msg'=>sview::$ctrl['msg'], 'attachs'=>sview::$ctrl['attachs'], 'version'=>sview::$ctrl['version'],
				'log'=>sview::$ctrl['log'], 'ip'=>get_ip(),  'status'=>0, 'stime'=>config::$nowtime);
		model::load('live/advise')->new_one($sdata);
		sview::$view->display();
	}
	
	//送完魅力之后回调请求0-1-5
	private static function __doclean() {
		sview::$ctrl['anchorid'] = intval(sview::$ctrl['anchorid']);
		if (sview::$ctrl['anchorid'] > 0) {
			$table  = model::load('live/anchor')->get_table();
			$szkey  = sprintf('%s/%s', $table, sview::$ctrl['anchorid']);
			$szkey  = model::get_skey($szkey);
			cache::getinst()->delete($szkey);
		}
		$userid = acl::getinst('vedio')->user['id'];
		if ($userid > 0) {
			$table  = model::load('live/userinfo')->set_devision($userid)->get_table();
			$szkey  = sprintf('%s/%s', $table, $userid);
			$szkey  = model::get_skey($szkey);
			cache::getinst()->delete($szkey);
		}
	}
	
	//收集主播端的帧率情况0-1-6
	private static function __doframe() {
		sview::$ctrl['studioid'] 	= intval(sview::$ctrl['studioid']);
		sview::$ctrl['eventid']  	= intval(sview::$ctrl['eventid']);
		sview::$ctrl['averfps']  	= floatval(sview::$ctrl['averfps']);
		sview::$ctrl['averflow'] 	= floatval(sview::$ctrl['averflow']);
		sview::$ctrl['avercamfps']  = floatval(sview::$ctrl['avercamfps']);
		sview::$ctrl['net']         = trim(sview::$ctrl['net']);
		sview::$ctrl['stime']    	= config::$nowtime;
		sview::$ctrl['agent']       = $_SERVER['HTTP_USER_AGENT'];
		sview::$ctrl['isbeauty'] 	= intval(sview::$ctrl['isbeauty']);
		if (sview::$ctrl['studioid'] && sview::$ctrl['eventid']) {
			model::load('live/stueventlog')->new_one(sview::$ctrl);
		}
		sview::$view->display();
	}
	
	//收集移动端奔溃日志0-1-9
	private static function __docrashlog() {
		sview::$ctrl['device']= trim(sview::$ctrl['device']);
		sview::$ctrl['os']    = trim(sview::$ctrl['os']);
		sview::$ctrl['ver']   = trim(sview::$ctrl['ver']);
		sview::$ctrl['msg']   = isset(sview::$ctrl['msg'])? sview::$ctrl['msg']:'';
		sview::$ctrl['msg']   = htmlspecialchars(sview::$ctrl['msg']);
		sview::$ctrl['file']  = isset(sview::$ctrl['file'])? sview::$ctrl['file']:'';
		$acl    = acl::getinst('vedio');
		$userid = ($acl->user)? $acl->user['id']:0;
		//新增一条记录信息
		$sdata = array('userid'=>$userid, 'device'=>sview::$ctrl['device'], 
			'os'=>sview::$ctrl['os'], 'file'=>sview::$ctrl['file'], 'msg'=>sview::$ctrl['msg'], 
			'ver'=>sview::$ctrl['ver'], 'ip'=>get_ip(), 'stime'=>config::$nowtime);
		model::load('live/appcrash')->new_one($sdata);
		sview::$view->display();
	}
	
	//收集移动端奔溃日志文件0-1-10
	private static function __docrashfile() {
		$ctype = isset($_SERVER['CONTENT_TYPE'])? $_SERVER['CONTENT_TYPE']:'plain/txt';
		if ($ctype && ($npos = strrpos($ctype, '/'))) {
			$ctype = '.'.substr($ctype, $npos+1);
		}
		$share = config::load('share');
		$path  = $share['dir']['cache'].'imlog/'.date('Ym').'/';
		if(!is_dir($path)) {
			mkdir($path, 0775, true);
		}
		$file  = $path.date('YmdHis').randme::rand_str(6).$ctype;
		file_put_contents($file, sview::$origin);
		$ip    = $_SERVER['SERVER_ADDR'];
		//这里各个服务器要配一下host /etc/host 域名绑定到本机的ip以便与获取IP
		if (preg_match('/^172/', $ip)) {
			$ip= gethostbyname($_SERVER['SERVER_NAME']);
		}
		$file  = $ip.':'.$file;
		sview::$view->assign('datasets', array('path'=>$file));
		sview::$view->display();
	}
	
	//上报错误日志 0-1-11
	private static function __doerrorlog() {
		sview::$ctrl['msg']   = isset(sview::$ctrl['msg'])? sview::$ctrl['msg']:'';
		sview::$ctrl['msg']   = htmlspecialchars(sview::$ctrl['msg']);
		$acl    = acl::getinst('vedio');
		$userid = ($acl->user)? $acl->user['id']:0;
		if (sview::$ctrl['msg'] && $userid) {
			$user = model::load('live/userinfo')->set_devision($userid)->get_one($userid);
			$user = $user['nickname'].'['.$userid.']';
			$field = array('type'=>1, 'request_uri'=>$_SERVER['REQUEST_URI'], 'user'=>$user,
					'msg'=>sview::$ctrl['msg'], 'ip'=>get_ip(), 'addtime'=>config::$nowtime);
			model::load('acl/syslog')->new_one($field);
		}
		sview::$view->display();
	} */
}